/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package wap.rockvilletech.xms;

/**
 * 
 * @author Tasnim
 */

public class SiWapPush {

	private static final String HEXINDEX = "0123456789abcdef          ABCDEF";

	public static String hexDump(byte[] buffer) {
		return hexDump(buffer, -1);
	}
	public static String hexDump(byte[] buffer, int len) {
		if (buffer == null)
			return null;
		if (len == 0) {
			return "";
		} else if (len < 0 || len > buffer.length) {
			len = buffer.length;
		}
		String dump = "";
		try {
			for (int i = 0; i < len; i++) {
				dump += Character.forDigit((buffer[i] >> 4) & 0x0f, 16);
				dump += Character.forDigit(buffer[i] & 0x0f, 16);
			}
		} catch (Throwable t) {
			t.printStackTrace(System.err);
			dump = null;
		}
		return dump;
	}
	public static String hexDump(String buffer) {
		return hexDump(buffer.getBytes(), -1);
	}
	public static byte[] hexToByte(String s) {
		int l = s.length() / 2;
		byte data[] = new byte[l];
		int j = 0;

		for (int i = 0; i < l; i++) {
			char c = s.charAt(j++);
			int n, b;

			n = HEXINDEX.indexOf(c);
			b = (n & 0xf) << 4;
			c = s.charAt(j++);
			n = HEXINDEX.indexOf(c);
			b += (n & 0xf);
			data[i] = (byte) b;
		}

		return data;
	}

	private String href;

	private String siId;

	private String action = "signal-medium";

	private String value;

	public SiWapPush(String href, String value) {
		setHref(href);
		setValue(value);
	}

	public String getAction() {
		if (action == null) {
			return "signal-medium";
		} else {
			return action;
		}
	}

	public String getDecodedAction() {
		if (action.compareToIgnoreCase("signal-none") == 0)
			return "05";
		if (action.compareToIgnoreCase("signal-low") == 0)
			return "06";
		if (action.compareToIgnoreCase("signal-high") == 0)
			return "08";
		if (action.compareToIgnoreCase("signal-delete") == 0)
			return "09";
		return "07";

	}

	public String getHref() {
		return href;
	}

	public String getSiId() {
		return siId;
	}

	public String getValue() {
		return value;
	}

	public String indicationToWBXML() {
		StringBuilder wbxml = new StringBuilder().append("c6".toUpperCase());
		if (siId != null && siId.length() > 0) {
			// si-id attribute
			wbxml.append("11");
			// string literal
			wbxml.append("03");
			// si-id string
			wbxml.append(hexDump(siId.getBytes()));
			// end string
			wbxml.append("00");
		}

		if (href != null && href.length() > 0) {
			if (href.startsWith("http://www.")) {
				wbxml.append("0d");
				href = href.substring(11);
			} else if (href.startsWith("https://www.")) {
				wbxml.append("0f");
				href = href.substring(12);
			} else if (href.startsWith("http://")) {
				wbxml.append("0c");
				href = href.substring(7);
			} else if (href.startsWith("https://")) {
				wbxml.append("0e");
				href = href.substring(8);
			}
			// String literal
			wbxml.append("03");
			wbxml.append(hexDump(href.getBytes()));
			// end string
			wbxml.append("00");

			wbxml.append(getDecodedAction());

			// >
			wbxml.append("01");
			// now the text
			wbxml.append("03");

			byte[] bytes = value.getBytes();
			wbxml.append(hexDump(bytes));
			wbxml.append("00");

		}
		wbxml.append("01");
		return wbxml.toString();
	}

	public void setAction(String value) {
		this.action = value;
	}

	public void setHref(String value) {
		this.href = value;
	}

	public void setSiId(String value) {
		this.siId = value;
	}

	public void setValue(String value) {
		this.value = value;
	}

	public String toSmsBinary() {
		/**
		 * GSM SMS User Data 06: UDH Length which is 6 05: 16 bit address 04:
		 * Length(4) 0B84: Destination Port(2948) 23F0: Source Port(9200)
		 * 
		 * Wireless Session Protocol 90: Transaction ID 06: PDU Type(Push) 01:
		 * Headers Length is 1 AE: Content-Type is application/vnd.wap.sic
		 */
		StringBuilder ud = new StringBuilder().append("DC0601AE"); // 0605040b8423f0
		ud.append(toWBXML());
		return ud.toString().toUpperCase();

	}

	public String toWBXML() {
		/**
		 * 02: Version number - WBXML version 1.2 05: SI 1.0 Public Identifier
		 * 6A: Charset=UTF-8 (MIBEnum 106) 00: String table length 45: si, with
		 * content
		 */
		StringBuilder wbxml = new StringBuilder().append("02056a0045".toUpperCase());
		wbxml.append(indicationToWBXML());
		wbxml.append("01");
		return wbxml.toString();
	}

	// public static void main(String[] args) {
	/*
	 * public int sendWAPPush(String wapPush, S){
	 * 
	 * SiWapPush wp = new
	 * SiWapPush("http://www.yxz.com/email/123/","Click to download"); String
	 * qry =
	 * "INSERT INTO spoolDir (provider, created, priority, xms) SELECT 1, NOW(), 1, '[SMPP188]\\nCount=0\\nOriginatingAddress=188\\nPhone=+923425185258\\nLocalId=root\\nDCS=245\\nUDH=05040B8423F0\\r\\nXMS="
	 * +wp.toSmsBinary()+"\\n' FROM providerId WHERE name='SMPP188';";
	 * System.out.println(qry); }
	 */

	/*
	 * WapSIPush wp = new WapSIPush(
	 * "http://www.yxz.com/email/123/","Click to download" );
	 * System.out.println( wp.toString() ); //wp.writeXmlTo( );
	 * SmsWapPushMessage pm = new SmsWapPushMessage(wp); byte[] buffer =
	 * pm.getUserData().getData(); System.out.println( hexDump(buffer));
	 * 
	 * }
	 */

}
